$(function () {
    var timer = null;
    var mySwiper = new Swiper('.swiper-container', {
        loop: true,
        pagination: '.swiper-pagination',
        autoplay: 2000,
    });

    newHtml();
    likePro();
    hotPro();
    newShopCartEvent();
    hotShopCartEvent();
    hotDownShopCartEvent();
    setCountDown();


    function setCountDown() {
        if (timer) {
            clearInterval(timer);
        }
        timer = setInterval(function () {
            var curtTime = getCountDown('2018-02-15 10:07:00');
            if (curtTime.isStop) {
                clearInterval(timer);
            }
            var h = curtTime.h;
            var m = curtTime.m;
            var s = curtTime.s;
            var str = '<p>' + h + '</p><span></span>'
                + '<p>' + m + '</p><span></span>'
                + '<p>' + s + '</p>';
            $('#countDown').html(str);
        }, 1000);
    }

    function newHtml() {
        qingQiuPhp('get', 'admin/index.php', {getType: 'gitNew', num: 6, type: 4}, function (res) {
            addNewHtml(res);
        });
    }

    function newShopCartEvent() {
        $('#newPro').delegate('button.shopCartBtn', 'tap', function () {
            var id = $(this).parents('li').attr('data-id');
            qingQiuPhp('post', 'admin/index.php', {postType: 'addNewOne', id: id, num: 1}, function (res) {
                alert(res.msg);
            });
        });
    }

    function hotPro() {
        qingQiuPhp('get', 'admin/index.php', {getType: 'gitHot', num: 4, type: 6}, function (res) {
            addHot(res);
        });

    }

    function hotShopCartEvent() {
        $('#hotProInfo').delegate('button.shopCartBtn', 'tap', function () {
            var id = $(this).siblings('p').attr('data-id');
            qingQiuPhp('post', 'admin/index.php', {postType: 'addHotOne', id: id, num: 1}, function (res) {
                alert(res.msg);
            });
        });
    }

    function hotDownShopCartEvent() {
        $('#hotProDown').delegate('button.shopCartBtn', 'tap', function () {
            var id = $(this).parents().attr('data-id');
            qingQiuPhp('post', 'admin/index.php', {postType: 'addHotDown', id: id, num: 1}, function (res) {
                alert(res.msg);
            });
        });
    }

    function likePro() {
        qingQiuPhp('get', 'admin/index.php', {getType: 'gitLike', num: 3, type: 5}, function (res) {
            addLikeHtml(res);
        });
    }

    function addNewHtml(res) {
        var newPro = $('#newPro');
        var str = '';
        for (var i = 0, n = res.length; i < n; i++) {
            str +=
                '<li data-id="' + res[i].id + '">'
                + '<p class="hornPic"></p>'
                + '<a href="pro_details.html?id=' + res[i].id + '">'
                + '<img src="' + res[i].imgSrc + '" alt="">'
                + '<p class="newInfo">' + res[i].title + '</p>'
                + '<p class="newInfo">大气灯具</p>'
                + '<p class="price">￥<span>' + res[i].price + '</span></p>'
                + '</a>'
                + '<button class="shopCartBtn"></button>'
                + '</li>';
        }
        newPro.html(str);
    }

    function addHot(datas) {
        var hotProFocus = '';
        var hotProInfo = '';
        var hotProDown = '';
        for (var i = 0, n = datas.length; i < n; i++) {
            if (i == 0) {
                hotProFocus +=
                    '<a href="pro_details.html?id=' + datas[i].id + '">'
                    + '<img src="' + datas[i].imgSrc + '" alt="">'
                    + '<div class="Angle"></div>'
                    + '<p class="hotProFocusInfo"><span>美式</span>' + datas[i].title + '</p>'
                    + '<div class="hotProPrice">'
                    + '<p class="price">￥<span>' + datas[i].price + '</span></p>'
                    + '<p class="prompt">火爆价</p>'
                    + '</div>'
                    + '</a>';
            } else if (i == 1) {
                hotProInfo +=
                    '<p data-id="' + datas[i].id + '" class="hornPic"></p>'
                    + '<a href="pro_details.html?id=' + datas[i].id + '">'
                    + '<img src="' + datas[i].imgSrc + '" alt="">'
                    + '<p class="newInfo">' + datas[i].title + '</p>'
                    + '<p class="newInfo">大气灯具</p>'
                    + '<p class="price">￥<span>' + datas[i].price + '</span></p>'
                    + '</a>'
                    + '<button class="shopCartBtn"></button>';
            } else {
                hotProDown +=
                    '<div data-id="' + datas[i].id + '" class="hotProDownMain">'
                    + '<p class="hornPic"></p>'
                    + '<a href="pro_details.html?id=' + datas[i].id + '">'
                    + '<img src="' + datas[i].imgSrc + '" alt="">'
                    + '<p class="newInfo">' + datas[i].title + '</p>'
                    + '<p class="price">￥<span>' + datas[i].price + '</span></p>'
                    + '</a>'
                    + '<button class="shopCartBtn"></button>'
                    + '</div>';
            }
        }
        $('#hotProFocus').html(hotProFocus);
        $('#hotProInfo').html(hotProInfo);
        $('#hotProDown').html(hotProDown);
    }

    function addLikeHtml(res) {
        var likePro = $('#likePro');
        var str = '';
        for (var i = 0, n = res.length; i < n; i++) {
            str +=
                '<div data-id="' + res[i].id + '" class="likeProList">'
                + '<a class="clearFix" href="pro_details.html?id=' + res[i].id + '">'
                + '<div class="Angle"></div>'
                + '<img src="' + res[i].imgSrc + '" alt="">'
                + '<p class="likeProInfo">' + res[i].title + '</p>'
                + '<p class="price">￥<span>' + res[i].price + '</span></p>'
                + '</a>'
                + '</div>'
        }
        likePro.html(str);
    }


});